/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyServlet;

import MyBean.MyConnection;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author quandz12
 */
public class Edit extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) //throws ServletException, IOException 
    {
        PrintWriter out = null;
        try //throws ServletException, IOException
        {
            response.setContentType("text/html;charset=UTF-8");
            out = response.getWriter();
            try {
                Connection con = MyConnection.getConnection();
                String code = request.getParameter("cat");
                Statement selectSt = con.createStatement();
                ResultSet rs = selectSt.executeQuery("Select * from [mobile].[dbo].[dienthoai] where dtID='" + code + "'");
                /*
                 * TODO output your page here. You may use following sample
                 * code.
                 */
                 ResultSet rs1 = selectSt.executeQuery("Select * from [mobile].[dbo].[hangSX]");
                while (rs.next()) {
                    out.println("<!DOCTYPE html>");
                    out.println("<html>");
                    out.println("<head>");
                    out.println("<title>Servlet CategoriesServlet</title>");
                    out.println("</head>");
                    out.println("<body>");
                    out.println("<form name=\"input\" action=\"EditDT?code= " + code + "\" method=\"POST\">");
                    out.println("<table>");
                    out.println("Ten dien thoai: <input name=\"name\" value=" + rs.getString("dtTen") + ">");
                    out.println("<tr>");
                    out.println("Man hinh: <input name=\"mh\" value=" + rs.getFloat("dtMH") + ">");
                    out.println("<tr>");
                    out.println("Gia: <input name=\"gia\" value=" + rs.getInt("dtGia") + ">");
                    out.println("<tr>");
                    out.println("<tr>");
                    while (rs1.next()) {
                    out.println("<select>");
                    out.println("<option name=\"hang\" value=" + rs1.getString("tenHang") + ">ten hang</option>");
                    out.println("</select>");
                    }
                    out.println("</tr>");
                    out.println("<input type=\"submit\" value=\"Save\" />");
                    out.println("</table>");
                    out.println("</form>");
                    out.println("</body>");
                    out.println("</html>");
                }
            } catch (SQLException ex) {
                Logger.getLogger(Edit.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Edit.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                out.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(Edit.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
